﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using FileSystem.Models;
using FileSystem.Services.IServices;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Xamarin.Forms;

namespace FileSystem.ViewModels {
    /// <summary>
    /// 用户页ViewModel
    /// </summary>
    public class UsersPageViewModel : ViewModelBase {
        // ******** 构造函数

        /// <summary>
        /// 内容页激活服务
        /// </summary>
        private IContentPageActivationService _contentPageActivationService;

        /// <summary>
        /// 内容导航服务
        /// </summary>
        private IContentNavigationService _contentNavigationService;

        /// <summary>
        /// 用户页ViewModel
        /// </summary>
        public UsersPageViewModel(
            IContentPageActivationService contentPageActivationService,
            IContentNavigationService contentNavigationService) {
            _contentPageActivationService = contentPageActivationService;
            _contentNavigationService = contentNavigationService;
        }

        // ******** 绑定属性

        /// <summary>
        /// 用户集合
        /// </summary>
        public ObservableCollection<UserViewModel> Users { get; } =
            new ObservableCollection<UserViewModel>();

        /// <summary>
        /// 菜单项参数
        /// </summary>
        public EventArgs MenuItemProperty {
            get => _menuItemproperty;
            set => Set(nameof(MenuItemProperty), ref _menuItemproperty, value);
        }

        /// <summary>
        /// 菜单项参数
        /// </summary>
        private EventArgs _menuItemproperty;

        // ******** 绑定命令

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        private RelayCommand _pageAppearingCommand;

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ?? (_pageAppearingCommand =
                new RelayCommand(() => PageAppearingCommandFunction()));

        public void PageAppearingCommandFunction() {
            if (_isLoaded)
                return;
            _isLoaded = true;

            // Users.Add(new UserViewModel { UserName = "小张", Image = "小张.png", UserLocation = "管理员", UserDetail = "小张是这台电脑的拥有者" });
            // Users.Add(new UserViewModel { UserName = "小北", Image = "小北.png", UserLocation = "普通用户", UserDetail = "小北是这小张的发小" });
            // Users.Add(new UserViewModel { UserName = "小花", Image = "小花.png", UserLocation = "普通用户", UserDetail = "小花是小张的暗恋对象" });
            // Users.Add(new UserViewModel { UserName = "小敏", Image = "小敏.png", UserLocation = "普通用户", UserDetail = "小敏是小张的追求者" });
            Users.Add(new UserViewModel {User = UserConst.Users[0]});
        }

        /// <summary>
        /// 登录命令
        /// </summary>
        private RelayCommand<User> _loginCommand;

        /// <summary>
        /// 登录命令
        /// </summary>
        public RelayCommand<User> LoginCommand =>
            _loginCommand ?? (_loginCommand =
                new RelayCommand<User>(async user =>
                    await LoginCommandFunction(user)));

        public async Task LoginCommandFunction(User user) {
            NavigationPage.SetHasNavigationBar(
                _contentPageActivationService.Activate(ContentNavigationConstants
                    .LoginPage), false);
            await _contentNavigationService.NavigateToAsync(
                ContentNavigationConstants.LoginPage, user);
        }


        // ******** 公开变量

        public static User CurrentUser = UserConst.Users[0];

        // ******** 私有变量

        /// <summary>
        /// 页面是否已经加载
        /// </summary>
        private volatile bool _isLoaded;
    }
}